<?php
/**
 * 卫生接口
 */
class Apiv1_HygieneController extends Ctrl_Apiatk{
	public function listAction(){
		$p = $_REQUEST;
		$tEMD = new ExamineModel;
		$tERMD = new ExamineregionModel;
		$tESMD = new ExaminescoreModel;
		$department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$status = empty($p['status'])?'':Tool_Fnc::safe_string($p['status']);
		$time = date('Y-m-d',time());
		$tDIDatas = $tEMD->field('id,exam_title,exam_picture')->where('e_id='.$status)->fList();
		foreach ($tDIDatas as $key => $tDatas) {
			// $tDIData = $tESMD->filed('cdatetime,score')->where("e_id=$tDatas['id'] and o_id=$department")->fList();
			// $Datas[$key]['type'] = $key;
			$tSql = 'select datetime,score from '.$tESMD->table.' where e_id='.$tDatas['id'].' and o_id='.$department.' and datetime="'.$time.'"'; 
			$datas = $tESMD->query($tSql);
			$Datas[$key]['content'] = $tDatas['exam_title']; 
			$Datas[$key]['photo'] = $tDatas['exam_picture'];
			$Datas[$key]['checktime'] = empty($datas[0]['datetime'])?'暂无':$datas[0]['datetime'];
			if (empty($datas)) {
				$Datas[$key]['score'] = "暂无";
			}else{
				$Datas[$key]['score'] = $datas[0]['score'].'分';
			}
		}
		Tool_Fnc::ajaxMsg('','1',$Datas);
	}
	/*卫生*/
	public function editAction(){
		// echo "1";
		$p = $_REQUEST;
		$tEMD = new ExamineModel;
		$tERMD = new ExamineregionModel;
		$tESMD = new ExaminescoreModel;
		$tOMD = new OverseerModel;
		$time = date('Y-m-d',time());
		$department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$status = empty($p['status'])?'':Tool_Fnc::safe_string($p['status']);
		$type = empty($p['type'])?'':Tool_Fnc::safe_string($p['type']);
		if ($type==1) {
			$ziduan = 'room';
		}elseif($type==2){
			$ziduan = 'hygiene';
		}elseif($type==3){
			$ziduan = 'toilet';
			$type = 8;
		}
		if ($status=='1') {
			$tDatas = $tOMD->field('id,date,sta,'.$ziduan)->where('department='.$department.' and date="'.$time.'"	')->fRow();
			if (empty($tDatas)) {
				$ODatas = array(
					'status' => 0,
					'department' => $department,
					'date' => $time,
					'sta' => $type+$tDatas['sta'],
				);
				if (!$tOMD->insert($ODatas)) {
					Tool_Fnc::ajaxMsg('提交失败','0');die;
				}
				Tool_Fnc::ajaxMsg('提交成功','1');die;
			}else{
				if ($tDatas['sta']==1&&$type==1||$tDatas['sta']==3&&$type==1||$tDatas['sta']==9&&$type==1||$tDatas['sta']==11&&$type==1||!empty($tDatas['room'])) {
					Tool_Fnc::ajaxMsg('居室卫生已提交,请勿重复提交','0');die;
				}if ($tDatas['sta']==8&&$type==8||$tDatas['sta']==9&&$type==8||$tDatas['sta']==10&&$type==8||$tDatas['sta']==11&&$type==8||!empty($tDatas['toilet'])){
					Tool_Fnc::ajaxMsg('卫生间卫生已提交,请勿重复提交','0');die;
				}if ($tDatas['sta']==2&&$type==2||$tDatas['sta']==3&&$type==2||$tDatas['sta']==10&&$type==2||$tDatas['sta']==11&&$type==2||!empty($tDatas['hygiene'])){
					Tool_Fnc::ajaxMsg('厨房卫生已提交,请勿重复提交','0');die;
				}
				$ODatas = array(
					'id' => $tDatas['id'],
					'sta' => $type+$tDatas['sta'],
				);
				if (!$tOMD->update($ODatas)) {
					Tool_Fnc::ajaxMsg('提交失败002','0',$ODatas);die;
				}
				Tool_Fnc::ajaxMsg('提交成功','1');die;
			}
		}
	}
	//厨房卫生
	public function hygieneAction(){
		$p = $_REQUEST;
		$tEMD = new ExamineModel;
		$tDIMO = new DepartmentinfoModel;
		// $tEDIDatas = $tDIMO->field('num,introduce')->fList();
		$tEDatas = $tEMD->field('id,exam_title,exam_content,exam_stand,exam_score')->where('e_id=2')->fList();
		// $tDatas['department'] = $tEDIDatas;
		$tDatas['hygiene'] = $tEDatas; 
		Tool_Fnc::ajaxMsg('','1',$tDatas);
	}
	//居室卫生
	public function roomAction(){
		$p = $_REQUEST;
		$tEMD = new ExamineModel;		
		$tDIMO = new DepartmentinfoModel;
		$tEDatas = $tEMD->field('id,exam_title,exam_content,exam_stand,exam_score')->where('e_id=1')->fList();
		// $tEDIDatas = $tDIMO->field('num,introduce')->fList();
		// $tDatas['department'] = $tEDIDatas;
		$tDatas['room'] = $tEDatas; 
		Tool_Fnc::ajaxMsg('','1',$tDatas);
	}
	//卫生间卫生
	public function toiletAction(){
		$p = $_REQUEST;
		$tEMD = new ExamineModel;
		$tDIMO = new DepartmentinfoModel;
		$tEDatas = $tEMD->field('id,exam_title,exam_content,exam_stand,exam_score')->where('e_id=3')->fList();
		// $tEDIDatas = $tDIMO->field('num,introduce')->fList();
		// $tDatas['department'] = $tEDIDatas;
		$tDatas['toilet'] = $tEDatas; 
		Tool_Fnc::ajaxMsg('','1',$tDatas);
	}
	//部门信息
	public function departmentAction(){
		$tDIMO = new DepartmentinfoModel;
		$tEDIDatas = $tDIMO->field('num,introduce')->fList();
		$tDatas['department'] = $tEDIDatas;
		Tool_Fnc::ajaxMsg('','1',$tDatas);
	}
	//督导检查
	public function addAction(){
		$q = $_REQUEST;
		$tESMD = new ExaminescoreModel;
		$tOSMD = new OverseerModel;
		$tEMD = new ExamineModel;
		$status = empty($q['status'])?'':Tool_Fnc::safe_string($q['status']);
		$sw_id = empty($q['sw_id'])?'':Tool_Fnc::safe_string($q['sw_id']);
		$question = empty($q['question'])?'':Tool_Fnc::safe_string($q['question']);
		$answer = empty($q['answer'])?'':Tool_Fnc::safe_string($q['answer']);
		$department = empty($q['department'])?'':Tool_Fnc::safe_string($q['department']);
		$EXData = $tEMD->field('id')->where('e_id='.$status)->fList();
		$time = date('Y-m-d',time());
		$Ques = explode(',',$question);
		$Ans = explode(',',$answer);
		foreach ($EXData as $key => $val) {
			$XDATA[$val['id']] = 0;
		}
		foreach ($Ques as $k => $Q) {
			$XDATA[$Q] = $Ans[$k];
		}
		$sum = 100-array_sum($Ans);
		$judge = count($EXData);
		if ($status==3) {
			$os='toilet';
			$status = 3;
		}else if ($status==2){
			$os='hygiene';
			$status = 2;
		}else{
			$os='room';
			$status = 1;
		}
		$tSql = 'select id,department,hygiene,room,toilet from '.$tOSMD->table.' where department='.$department.' and date="'.$time.'"';
		$tODatas = $tOSMD->query($tSql);
		if (!empty($tODatas)) {
			if (empty($tODatas[0][$os])) {
				$Odata = array(
					'department' => $department,
					'date' => $time,
					"$os" => $sum,
					'checktime' => $time,
					'overseer' => $sw_id,
					'status' => $status,
					'id' => $tODatas[0]['id'],
				);
				if (!$tOSMD->update($Odata)) {
					Tool_Fnc::ajaxMsg("提交失败001",'0',$tSql);
				}
			}else{
					Tool_Fnc::ajaxMsg("今日已提交",'0');die;
			}
		}else{
				$Odata = array(
				'department' => $department,
				'date' => $time,
				"$os" => $sum,
				'checktime' => $time,
				'overseer' => $sw_id,
				'status' => $status,
				);
				if (!$tOSMD->insert($Odata)) {
						Tool_Fnc::ajaxMsg("提交失败002",'0');
				}
		}
		$sql = 'select max(id) from '.$tOSMD->table.' where department='.$department.' and date="'.$time.'" and overseer='.$sw_id;
		$ODatas = $tOSMD->query($sql);
		foreach ($XDATA as $key => $val) {
			$Odata = array(
				'e_id'=>$key,
				'times'=>1,
				'score'=>$val,
				'datetime'=>"$time",
				'o_id'=>$department,
				'x_id'=>$ODatas[0]['max(id)'],	
			);
			if($tESMD->insert($Odata)){
				$num++;	
			};	
			
		}
		if ($num==$judge+1) {
				Tool_Fnc::ajaxMsg('提交成功','1');	
			}else{
				Tool_Fnc::ajaxMsg('提交失败003','0',$judge.$num);	
			}
	}
}